package com.example.spsaveqqty.attraction;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SearchView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.example.spsaveqqty.EditUserInfoActivity;
import com.example.spsaveqqty.PersonalCenterActivity;
import com.example.spsaveqqty.R;
import com.example.spsaveqqty.guides.GuidesMainActivity;
import com.example.spsaveqqty.map_weather;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class AtractionsActivity extends AppCompatActivity implements View.OnClickListener{

    private SearchView mSearch;
    List<Integer> imageUrlData;
    List<String> contentData;
    private TextView tv;
    private TextView lable_rank;

    //排行榜
    private LinearLayout oneRank;
    private ImageView iv_pic;
    private TextView tv_name;
    private TextView tv_rank;
    private TextView tv_tip;
    private LinearLayout ll_rank;
    List<Attraction> rkList = null;
    private ImageButton ib_map,ib_guide;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_atractions);

        banner();

        //排行列表
        Intent i = getIntent();

        rkList  = (List<Attraction>) i.getSerializableExtra("rkList");


        tv = findViewById(R.id.lable_search);
        tv.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AtractionsActivity.this, SearchActivity.class);
                startActivity(intent);
            }
        });

        lable_rank = findViewById(R.id.lable_rank);
        lable_rank.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {

                AttractionDao dao = new AttractionDao(AtractionsActivity.this);
                List<Attraction> rankList = null;
                rankList = dao.selectByOrder();
//                Log.i("-------------------", rankList.get(0).toString());
                Intent intent = new Intent(AtractionsActivity.this, RankActivity.class);
                intent.putExtra("rankList", (Serializable) rankList);
                startActivity(intent);

            }
        });


        ib_map = findViewById(R.id.imageButton1);//导航栏的地图主页按钮
        ib_map.setOnClickListener(this);
        ib_guide = findViewById(R.id.imageButton2);//导航栏的景区主页按钮
        ib_guide.setOnClickListener(this);


        //排行榜

        rankList(rkList);

    }

    //导航栏点击事件处理
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.imageButton1://地图按钮
            {
                Intent intent = new Intent(AtractionsActivity.this, map_weather.class);
                startActivity(intent);
                AtractionsActivity.this.finish();//关闭当前页面
            }
            break;
            case R.id.imageButton2://攻略按钮
            {
                Intent intent = new Intent(AtractionsActivity.this, GuidesMainActivity.class);
                startActivity(intent);
                finish();
            }
        }
    }

    public void rankList(List<Attraction> rankList) {

        ll_rank = findViewById(R.id.ll_rank);

        for (int i = 0; i < 3; i++) {
            oneRank = new LinearLayout(this);
            iv_pic = new ImageView(this);
            tv_name = new TextView(this);
            tv_rank = new TextView(this);
            tv_tip = new TextView(this);

            initView(oneRank, iv_pic);

            Glide.with(this).load(rankList.get(i).getPic()).into(iv_pic);
            tv_name.setText(rankList.get(i).getAttract_name());
            tv_name.setTextSize(20);
            tv_name.setGravity(Gravity.CENTER);
            tv_rank.setText("全国景点人气排行第「" + (i+1) + "」名");
            tv_rank.setGravity(Gravity.CENTER);
            tv_tip.setText(rankList.get(i).getTip());
            tv_tip.setGravity(Gravity.CENTER);

            // 使用代码设置drawableleft
            Drawable drawable = getResources().getDrawable(
                    R.drawable.icon_rk);
            // / 这一步必须要做,否则不会显示.
            drawable.setBounds(0, 0, drawable.getIntrinsicHeight(),
                    drawable.getIntrinsicWidth());
            tv_rank.setCompoundDrawables(drawable, null, null, null);
            LinearLayout.LayoutParams par = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            par.setMargins(0,20,0,0);
            tv_rank.setLayoutParams(par);
            tv_tip.setLayoutParams(par);

            oneRank.addView(iv_pic);
            oneRank.addView(tv_name);
            oneRank.addView(tv_rank);
            oneRank.addView(tv_tip);

            ll_rank.addView(oneRank);

        }
    }

    public void initView(LinearLayout oneRank, ImageView iv_pic) {

        LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(500, 800);
        param.setMargins(20, 0, 0, 0);
        oneRank.setLayoutParams(param);
        oneRank.setOrientation(LinearLayout.VERTICAL);

        LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                400);
        iv_pic.setLayoutParams(param2);
        iv_pic.setScaleType(ImageView.ScaleType.FIT_XY);
    }

    //轮播
    public void banner(){
        imageUrlData = new ArrayList<>();
        contentData = new ArrayList<>();

        imageUrlData.add(R.drawable.attrac1);
        imageUrlData.add(R.drawable.attrac2);
        imageUrlData.add(R.drawable.attrac3);
        contentData.add("苍山之下，洱海之滨，风花雪月");
        contentData.add("野生动物的乐园 ———— 可可西里");
        contentData.add("峨眉风光天下秀，普贤菩萨道场");

        Banner banner = (Banner) findViewById(R.id.banner);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE_INSIDE);

        //设置图片集合
        banner.setImages(imageUrlData);
        banner.setBannerTitles(contentData);
        banner.setBannerAnimation(Transformer.Default);
        //切换频率
        banner.setDelayTime(2000);
        //自动启动
        banner.isAutoPlay(true);
        //位置设置
        banner.setIndicatorGravity(BannerConfig.CENTER);

        //banner设置方法全部调用完毕时最后调用
        banner.start();

    }

    //使按返回键时直接返回个人中心：
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //点击home键
        if (keyCode == KeyEvent.KEYCODE_HOME) {
            return true;
        }
        //点击返回键
        else if (keyCode == KeyEvent.KEYCODE_BACK) {
            Intent intent = new Intent(AtractionsActivity.this, GuidesMainActivity.class);
            startActivity(intent);
            setResult(RESULT_OK, intent);
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
